package com.flower.hooc.service.impl;

import com.flower.hooc.bean.pojo.entity.User;
import com.flower.hooc.bean.pojo.qo.UserLoginQO;
import com.flower.hooc.mapper.UserMapper;
import com.flower.hooc.service.UserService;
import com.flower.hooc.util.exception.ShopException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author:kevinfoo
 * @date:2020/08/28
 * @file:com.flower.service.impl
 */
@Service
public class UserServiceImpl implements UserService {

    private Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Autowired
    private UserMapper userMapper;

    @Override
    public User getById(Long userId) {
        User user = userMapper.selectById(userId);
        if (user == null) {
            logger.warn("getById user not exist [userId:{}]", userId);
            throw new RuntimeException("用户不存在");
        }
        return user;
    }

    @Override
    public User register(User user) {
        //插入用户
        int result = userMapper.addUser(user);
        //查询用户
        User dbUser = userMapper.selectByUserName(user.getName());
        return dbUser;
    }

    @Override
    public User getByUserName(String username) {
        User user = userMapper.selectByUserName(username);
        if (user == null) {
            logger.warn("getByUserName user not exist [username:{}]", username);
            throw new ShopException("用户不存在");
        }
        return user;
    }

    @Override
    public User login(UserLoginQO qo) {
        //查询用户
        User dbUser = userMapper.selectByUserName(qo.getUserName());

        return dbUser;
    }

}
